var express = require('express');
var crypto = require('crypto');
var router = express.Router();
var mainRender = require('../render/mainRender.jsx');
var productRender = require('../render/productRender.jsx');
var aboutRender = require('../render/aboutRender.jsx');
var viewPointRender = require('../render/viewPointRender.jsx');
var productDetailRender = require('../render/productDetailRender.jsx');
var fundDetailRender = require('../render/fundDetailRender.jsx');
var registerRender = require('../render/registerRender.jsx');
var loginRender = require('../render/loginRender.jsx');
var computeRender = require('../render/computeRender.jsx');
var myAccountRender = require('../render/myAccountRender.jsx');
var commonDetailRender = require('../render/commonDetailRender.jsx');
var updateBrowserRender = require('../render/updateBrowserRender.jsx');

var GetData = require('./getData.js');
var getData = new GetData();

//调用api访问类，初始化api
router = getData.initGetDataRouter(router);

/* GET home page. */
router.get('/', function (req, res, next) {
    var mainHome = mainRender;
    mainRender.RenderFunc((header, bar, footer, main, initializeStore)=> {
        res.render('react/common', {
            title: '华康海宇财富管理有限公司',
            reactOutput: main,
            headOutput: header,
            barOutput: bar,
            footOutput: footer,
            layout: 'hkfsLayout',
            initializeStore: initializeStore,
            index: 'index',
            myModel: 'model'
        });
    },req.session.userId);
});

router.get('/products', function (req, res, next) {
    var productHome = productRender;
    productHome.RenderFunc((header, bar, footer, main, initializeStore)=> {
        res.render('react/common', {
            title: '华康产品',
            reactOutput: main,
            headOutput: header,
            barOutput: bar,
            footOutput: footer,
            layout: 'hkfsLayout',
            initializeStore: initializeStore,
            index: 'products',
            myModel: 'model'
        });
    },req.session.userId);
});

router.get('/about', function (req, res, next) {
    var aboutHome = aboutRender;
    aboutHome.RenderFunc((header, bar, footer, main, initializeStore)=> {
        res.render('react/common', {
            title: '关于我们',
            reactOutput: main,
            headOutput: header,
            barOutput: bar,
            footOutput: footer,
            layout: 'hkfsLayout',
            initializeStore: initializeStore,
            index: 'about',
            myModel: 'model'
        });
    });
});

router.get('/viewPoint', function (req, res, next) {
    var viewPointHome = viewPointRender;
    viewPointHome.RenderFunc((header, bar, footer, main, initializeStore)=> {
        res.render('react/common', {
            title: '华康视点',
            reactOutput: main,
            headOutput: header,
            barOutput: bar,
            footOutput: footer,
            layout: 'hkfsLayout',
            initializeStore: initializeStore,
            index: 'viewPoint',
            myModel: 'model'
        });
    });
});

router.get('/productDetail', function (req, res, next) {
    var productDetailHome = productDetailRender;
    productDetailHome.RenderFunc(req.query.parId, (header, bar, footer, main, initializeStore)=> {
        res.render('react/common', {
            title: '产品详情',
            reactOutput: main,
            reactOutput: main,
            headOutput: header,
            barOutput: bar,
            footOutput: footer,
            layout: 'hkfsLayout',
            initializeStore: initializeStore,
            index: 'productDetail',
            myModel: 'model'
        });
    });
});

router.get('/fundDetail', function (req, res, next) {
    var fundDetailHome = fundDetailRender;
    fundDetailHome.RenderFunc(req.query.fundCode, (header, bar, footer, main, initializeStore)=> {
        res.render('react/common', {
            title: '基金详情',
            reactOutput: main,
            reactOutput: main,
            headOutput: header,
            barOutput: bar,
            footOutput: footer,
            layout: 'hkfsLayout',
            initializeStore: initializeStore,
            index: 'fundDetail',
            myModel: 'model'
        });
    });
});

router.get('/register', function (req, res, next) {
    var registeHome = registerRender;
    registeHome.RenderFunc((header, bar, footer, main, initializeStore)=> {
        res.render('react/common', {
            title: '免费注册',
            reactOutput: main,
            reactOutput: main,
            headOutput: header,
            barOutput: bar,
            footOutput: footer,
            layout: 'hkfsLayout',
            initializeStore: initializeStore,
            index: 'register',
            myModel: 'specialModel'
        });
    });
});

router.get('/login', function (req, res, next) {
    /*req.session.userId='';*/
    var loginHome = loginRender;
    loginHome.RenderFunc((header, bar, footer, main, initializeStore)=> {
        res.render('react/common', {
            title: '请登录',
            reactOutput: main,
            reactOutput: main,
            headOutput: header,
            barOutput: bar,
            footOutput: footer,
            layout: 'hkfsLayout',
            initializeStore: initializeStore,
            index: 'login',
            myModel: 'specialModel'
        });
    });
});

router.get('/compute', function (req, res, next) {
    var computeHome = computeRender;
    computeHome.RenderFunc((main, initializeStore)=> {
        res.render('react/common', {
            title: '收益计算',
            reactOutput: main,
            layout: 'emptyLayout',
            initializeStore: initializeStore,
            index: 'hkfsCompute'
        });
    });
});

router.get('/updateYourBrowser',function(req,res,next){
    updateBrowserRender.RenderFunc((main, initializeStore)=> {
        res.render('react/common', {
            title: '请升级你的浏览器',
            reactOutput: main,
            layout: 'emptyLayout',
            initializeStore: initializeStore,
            index: 'updateBrowser'
        });
    });
});

router.get('/myAccount', function (req, res, next) {
    if (req.session.userId) {
        var myAccountHome = myAccountRender;
        myAccountHome.RenderFunc(req.session.mobile,(header, bar, footer, main, initializeStore)=> {
            res.render('react/common', {
                title: '我的账户',
                reactOutput: main,
                headOutput: header,
                barOutput: bar,
                footOutput: footer,
                layout: 'hkfsLayout',
                initializeStore: initializeStore,
                index: 'hkfsMyAccount',
                myModel: 'model'
            });
        });
    }
    else {
        res.redirect('/login');
    }
});


//获取详情公用
router.get('/hkfsDetail', function (req, res, next) {
    commonDetailRender.RenderFunc((header, bar, footer, main, initializeStore)=> {
        res.render('react/common', {
            title: '详情',
            reactOutput: main,
            headOutput: header,
            barOutput: bar,
            footOutput: footer,
            layout: 'hkfsLayout',
            initializeStore: initializeStore,
            index: 'hkfsDetail',
            myModel: 'model'
        });
    });
});

router.post('/api/test', function (req, res) {
    res.response();
    /*console.log('sdssssf');
     res.json('sdf');*/
});


module.exports = router;
